"""
Document processing module for parsing and chunking documents.
"""
import io
from typing import List, Dict
from pypdf import PdfReader


class DocumentProcessor:
    """Processes documents and splits them into chunks."""
    
    def __init__(self, chunk_size: int = 1000, chunk_overlap: int = 200):
        """
        Initialize document processor.
        
        Args:
            chunk_size: Maximum size of each chunk in characters
            chunk_overlap: Overlap between chunks in characters
        """
        self.chunk_size = chunk_size
        self.chunk_overlap = chunk_overlap
    
    def process_file(
        self,
        content: bytes,
        filename: str,
        file_id: str,
        file_ext: str
    ) -> List[Dict[str, any]]:
        """
        Process a file and return chunks.
        
        Args:
            content: File content as bytes
            filename: Original filename
            file_id: Unique file identifier
            file_ext: File extension (e.g., '.pdf')
        
        Returns:
            List of chunk dictionaries with 'text' and 'metadata' keys
        """
        # Extract text based on file type
        if file_ext == ".pdf":
            text = self._extract_pdf_text(content)
        elif file_ext in {".txt", ".md"}:
            text = content.decode("utf-8", errors="ignore")
        else:
            raise ValueError(f"Unsupported file extension: {file_ext}")
        
        # Split into chunks
        chunks = self._split_text(text, filename, file_id)
        
        return chunks
    
    def _extract_pdf_text(self, content: bytes) -> str:
        """Extract text from PDF file."""
        pdf_file = io.BytesIO(content)
        reader = PdfReader(pdf_file)
        text_parts = []
        
        for page in reader.pages:
            text_parts.append(page.extract_text())
        
        return "\n\n".join(text_parts)
    
    def _split_text(
        self,
        text: str,
        filename: str,
        file_id: str
    ) -> List[Dict[str, any]]:
        """
        Split text into overlapping chunks.
        
        Args:
            text: Text to split
            filename: Original filename
            file_id: Unique file identifier
        
        Returns:
            List of chunk dictionaries
        """
        if not text.strip():
            return []
        
        chunks = []
        start = 0
        chunk_index = 0
        
        while start < len(text):
            # Calculate end position
            end = start + self.chunk_size
            
            # Extract chunk
            chunk_text = text[start:end]
            
            # Create chunk metadata
            chunk = {
                "text": chunk_text.strip(),
                "metadata": {
                    "file_id": file_id,
                    "filename": filename,
                    "chunk_index": chunk_index,
                    "start": start,
                    "end": min(end, len(text))
                }
            }
            
            chunks.append(chunk)
            
            # Move start position with overlap
            start = end - self.chunk_overlap
            chunk_index += 1
            
            # Avoid infinite loop
            if start >= len(text):
                break
        
        return chunks

